package com.zhupf.dynamic;


/**
 * @author zhupf
 * @date 2024年04月11日 10:05
 * @Description
 * 152. 乘积最大子数组
 */
public class MaxProduct {

    public static void main(String[] args) {
        int[] nums = {-4,-3,-2};
        System.out.println(maxProduct(nums));
    }

    public static int maxProduct(int[] nums) {
        int preMax = nums[0];
        int preMin = nums[0];
        int max = preMax;
        for (int i = 1; i < nums.length; i++) {
            if(nums[i]>0){
                preMax = Math.max(nums[i]*preMax,nums[i]);
                preMin = Math.min(nums[i]*preMin,nums[i]);
            }else {
                int tmp = preMax;
                preMax = Math.max(nums[i]*preMin,nums[i]);
                preMin = Math.min(nums[i]*tmp,nums[i]);
            }
            max = Math.max(preMax,max);
        }
        return max;
    }

}




